<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<p id="support">你的浏览器不支持 HTML5 Web Workers</p>
<button id="stopButton" >停止任务</button>
<button id="helloButton" >发送消息</button>
<script>
function stopWorker() {
    worker.terminate(); 
}
function messageHandler(e) {
    console.log(e.data);
}
function errorHandler(e) {
    console.warn(e.message, e);
}
function loadDemo() {
    if( typeof (Worker) !== "undefined") {
        document.getElementById("support").innerHTML = "你的浏览器支持HTML5 Web Workers";
        worker = new Worker("worker.js");
        worker.addEventListener("message", messageHandler, true);
        worker.addEventListener("error", errorHandler, true);
        document.getElementById("helloButton").onclick = function() {
            worker.postMessage("ok");
        }
        document.getElementById("stopButton").onclick = stopWorker;
    }
}
window.addEventListener("load", loadDemo, true);
</script>
